package ift604.projet.oauth;


import java.io.BufferedReader;
import java.io.InputStreamReader;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;

public class OAuthManager implements OAuthCallback {
    public Activity mainActivity;
    String consumerKey;
    String consumerSecret;
    String requestURL;
    String accessURL;
    String authorizeURL;
    String callbackScheme;
    String callbackHost;
    OAuthCallback callback;
    boolean authenticated = false;

    public OAuthConsumer consumer;
    public OAuthProvider provider;


    public OAuthManager(Activity mainActivity, String consumerKey, String consumerSecret, String requestURL, String accessURL, String authorizeURL,
                        String callbackScheme, String callbackHost) {
        this.mainActivity = mainActivity;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.requestURL = requestURL;
        this.accessURL = accessURL;
        this.authorizeURL = authorizeURL;
        this.callbackScheme = callbackScheme;
        this.callbackHost = callbackHost;

        this.consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
        this.provider = new CommonsHttpOAuthProvider(requestURL, accessURL, authorizeURL);

    }

    public void authenticate(OAuthCallback callback) {
        this.callback = callback;
        OAuthLoginActivity.oAuthManager = this;
        Intent intent = new Intent(mainActivity.getApplicationContext(), OAuthLoginActivity.class);
        mainActivity.startActivity(intent);
    }

    public String sendRequest(HttpPost request) {
        StringBuilder builder = new StringBuilder();
        try {
            consumer.sign(request);

            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(request);

            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            for (String line = null; (line = reader.readLine()) != null; ) {
                builder.append(line).append("\n");
            }

            return builder.toString();
        } catch (Exception e) {
        	return null;
        }
    }

    public String sendRequestToURL(String URL) {
        HttpPost request = new HttpPost(URL);
        return sendRequest(request);
    }


    public boolean isAuthenticated() {
        return authenticated;
    }

    @Override
    public void authenticationSuccess() {
        authenticated = true;
        if (callback != null) {
            callback.authenticationSuccess();
        }
    }

    @Override
    public void authenticationFailure() {
        if (callback != null) {
            callback.authenticationFailure();
        }
    }

}
